import axios from "axios"

import store from '../stores'
import router from '../router'

const service = axios.create({
    baseURL: "http://localhost:8081",
    timeout: 10000
})
// 请求之前 拦截
service.interceptors.request.use((config) => {
    if(store.useUserStore.token){
        //带上token再发
        config.headers['token'] = store.useUserStore.token
    }else{
        router.push("/login")
    }
    return config;
}, (error) => {
    Promise.reject(error)
})
// 请求之后，拦截
service.interceptors.response.use(
    (res) => {
        // console.log(res)
        if(res.status === 200){
            return Promise.resolve(res.data)
        }

    }, (error) => {
        return Promise.reject(error)
    }
)
export default service;